package com.wufun.overseas.utils;

import java.util.Calendar;

/**
 * User: mawanjin@join-cn.com
 * Date: 14-9-17
 * Time: 上午11:15
 */
public class DateUtils {

    /**
     * 下载日期是几分钟前、几天前
     *
     * @return
     */
    public static String FormatForDownloadTime(long begin) {
        long second = 1000;//1s=1000ms
        long minute = second * 60;
        long hour = minute * 60;
        long day = hour * 24;
        long month = day * 30;
        long year = month * 12;

        long now = System.currentTimeMillis();
        long gap = now - begin;
        if (gap < minute)
            return "Just now"; //小于1分钟
        else if (gap < hour)
            return (gap / minute < 2) ? gap / minute + " Min ago" : gap / minute + " Mins ago";   //小于1小时
        else if (gap < day)
            return (gap / hour < 2) ? gap / hour + " Hour ago" : gap / hour + " Hours ago";//小于24小时
        else if (gap < month)
            return (gap / day < 2) ? gap / day + " Day ago" : gap / day + " Days ago";  //1天前，1一个月以内
        else if (gap < year)
            return (gap / month < 2) ? gap / month + " Month ago" : gap / month + " Months ago";   //1天前，1一个月以内
        else
            return (gap / year < 2) ? gap / year + " Year ago" : gap / year + " Years ago";// 1年前

    }

    /**
     * 下载日期是几分钟前、几天前
     *
     * @return
     */
    public static String FormatForDownloadTimeDetail(long begin) {
        long second = 1000;//1s=1000ms
        long minute = second * 60;
        long hour = minute * 60;
        long day = hour * 24;
        long month = day * 30;
        long year = month * 12;

        long now = System.currentTimeMillis();
        long gap = now - begin;
        if (gap < minute) return "刚刚"; //小于1分钟
        else if (gap < hour) return gap / minute + "分钟前";   //小于1小时
        else if (gap < day) return gap / hour + "小时前";//小于24小时
        else if (gap < month) return gap / day + "天前";  //1天前，1一个月以内
        else if (gap < year) return gap / month + "月前";   //1天前，1一个月以内
        else return gap / year + "年前";// 1年前

    }

    /**
     * 下载日期是几分钟前、几天前
     *
     * @return
     */
    public static String TimeToNowFucher(long begin) {
        long second = 1000;//1s=1000ms
        long minute = second * 60;
        long hour = minute * 60;
        long day = hour * 24;
        long month = day * 30;
        long year = month * 12;

        long now = System.currentTimeMillis();
        long gap = begin - now;
        if (isToday(begin)) {
            return "今天";
        }
        if (gap < 0) {
            gap = now - begin;
            if (gap < minute) return "今天"; //小于1分钟
            else if (gap < hour) return "今天";   //小于1小时
            else if (gap < day) return "昨天";//小于24小时
            else if (gap < month) return gap / day + "天前";  //1天前，1一个月以内
            else if (gap < year) return gap / month + "月前";   //1天前，1一个月以内
            else return gap / year + "年前";// 1年前
        } else {
            if (gap < hour) return "今天";   //小于1小时
            else if (gap < day) return "明天";//小于24小时
            else if (gap < month) return gap / day + "天后";  //1天前，1一个月以内
            else if (gap < year) return gap / month + "月后";   //1天前，1一个月以内
            else return gap / year + "年后";// 1年前

        }
//        if (gap < minute) return ""; //小于1分钟

    }

    /**
     * 下载日期是几分钟前、几天前
     *
     * @return
     */
    public static String TimeToNowFucherDeail(long begin) {
        long second = 1000;//1s=1000ms
        long minute = second * 60;
        long hour = minute * 60;
        long day = hour * 24;
        long month = day * 30;
        long year = month * 12;

        long now = System.currentTimeMillis();
        long gap = begin - now;
        if (gap < 0) return "已开服";
//        if (gap < minute) return ""; //小于1分钟
        else if (gap < hour) return gap / minute + "天";   //小于1小时
        else if (gap < day) return gap / hour + "天";//小于24小时
        else if (gap < month) return gap / day + "天";  //1天前，1一个月以内
        else if (gap < year) return gap / month + "月";   //1天前，1一个月以内
        else return gap / year + "年";// 1年前

    }


    /**
     * 是否是今天
     *
     * @param time
     * @return
     */
    public static boolean isToday(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DATE);

        calendar.setTimeInMillis(System.currentTimeMillis());
        int nYear = calendar.get(Calendar.YEAR);
        int nMonth = calendar.get(Calendar.MONTH);
        int nDay = calendar.get(Calendar.DATE);

        return year == nYear && month == nMonth && day == nDay;


    }


    public static String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
    }
}
